package com.party.service.sys.impl;

import com.party.common.Page;
import com.party.common.Tools;
import com.party.mapper.SysUserMapper;
import com.party.model.SysUser;
import com.party.model.SysUserExample;
import com.party.service.sys.SysUserService;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 13-3-31
 * Time: 下午3:15
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class SysUserServiceImpl implements SysUserService {
    @Resource
    SysUserMapper sysUserMapper;

    public Page getUserList(SysUser sysUser, int pageSize, int pageNumber) {
        //组合查询的条件
        SysUserExample sysUserExample = new SysUserExample();

        if(sysUser != null)
        {
            SysUserExample.Criteria criteria = sysUserExample.createCriteria();
            if(sysUser.getPassport() != null && !"".equals(sysUser.getPassport()))
            {
                criteria.andPassportLike("%"+sysUser.getPassport()+"%");
            }
            if(sysUser.getRegtime() != null && !"".equals(sysUser.getRegtime()))
            {
                criteria.andRegtimeGreaterThan(sysUser.getRegtime()+" 00:40:16");
            }
            if(sysUser.getRealname() != null && !"".equals(sysUser.getRealname()))
            {
                criteria.andRealnameLike("%"+sysUser.getRealname()+"%");
            }
        }

        //获取总的记录数量
        int count = this.sysUserMapper.countByExample(sysUserExample);
        //获取指定的页码数据
        sysUserExample.setBeginNum((pageNumber - 1) * pageSize);
        sysUserExample.setEndNum(pageSize);
        List<SysUser> list = this.sysUserMapper.getListPage(sysUserExample);
        //组合返回page
        Page page = new Page(pageNumber, count, pageSize, list);
        return page;
    }

    public int add(SysUser sysUser) {
        sysUser.setRegtime(Tools.getNowDate());
        return sysUserMapper.insertSelective(sysUser);
    }

    public int del(String ids) {
        int num = 0;
        if (ids != null) {
            String uids[] = ids.split(",");
            for (String uid : uids) {
                num += sysUserMapper.deleteByPrimaryKey(Integer.parseInt(uid));
            }
        }
        return num;
    }

    public SysUser getUser(int uid) {
        return sysUserMapper.selectByPrimaryKey(uid);
    }

    public SysUser getUser(SysUser sysUser) {
        SysUserExample sysUserExample = new SysUserExample();
        SysUserExample.Criteria criteria = sysUserExample.createCriteria();
        if (sysUser.getPassport() != null) {
            criteria.andPassportEqualTo(sysUser.getPassport());
        }
        List<SysUser> sysUsers = sysUserMapper.selectByExample(sysUserExample);
        if (sysUsers.size() > 0) {
            return sysUsers.get(0);
        }
        return null;
    }

    public int changePwd(SysUser sysUser) {
        return sysUserMapper.updateByPrimaryKey(sysUser);
    }

    public int update(SysUser sysUser) {
        if(sysUser == null)
        {
            return 0;
        }
        if(sysUser.getUid() ==null || sysUser.getUid() <= 0)
        {
            return 0;
        }
        return sysUserMapper.updateByPrimaryKeySelective(sysUser);
    }

    @Override
    public List<SysUser> getUserListBylOne(SysUser sysUser) {
        //组合查询的条件
        SysUserExample sysUserExample = new SysUserExample();

        if(sysUser != null)
        {
            SysUserExample.Criteria criteria = sysUserExample.createCriteria();
            if(sysUser.getLone() != null && !"".equals(sysUser.getLone()))
            {
                criteria.andLoneEqualTo(sysUser.getLone());
            }
        }

        List<SysUser> list = this.sysUserMapper.getUserListBylOne(sysUserExample);
        //组合返回page
        return list;
    }

    @Override
    public List<SysUser> getAllUsers() {
        SysUserExample sysUserExample = new SysUserExample();
        List<SysUser> list = this.sysUserMapper.getAllUsers(sysUserExample);
        return list;
    }
}
